package org.example.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * mock服务代理
 */
@Slf4j
public class MockServiceProxy implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Class<?> returnType = method.getReturnType();
        log.info("方法是,{}",method.getName());
        return returnDefaultValue(returnType);
    }

    /**
     * 根据返回值类型返回默认值
     * @param returnType
     * @return
     */
    public Object returnDefaultValue(Class<?> returnType) {
        if (returnType.equals(int.class)) {
            return 0;
        } else if (returnType.equals(boolean.class)) {
            return false;
        } else if (returnType.equals(long.class)) {
            return 0L;
        } else if (returnType.equals(double.class)) {
            return 0.0;
        } else if (returnType.equals(float.class)) {
            return 0.0f;
        }
        return null;
    }
}
